package com.admin.action

import com.admin.service.ThemeExamineService
import com.data.annotation.NoargAnnotation
import com.data.annotation.OpenAnnotation
import com.data.util.JsonUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseBody
import javax.servlet.http.HttpServletRequest

@Controller
@OpenAnnotation
@NoargAnnotation
@RequestMapping("themeExamine/admin/")
class ThemeExamineAction {
    /***
     * 查看审核列表
     */
    @Autowired
    private var themeExamineService: ThemeExamineService? = null

    @RequestMapping("list")
    fun listExamineThemeAction(request: HttpServletRequest): String {
        val list = this.themeExamineService!!.listExamineThemeService()
        request.setAttribute("dataMap", list)
        return "WEB-INF/admin/examine/listExamine"
    }

    @RequestMapping("listMassage")
    @ResponseBody
    fun listMassageExamineThemeAction(request: HttpServletRequest): Map<String, Any> {
        val list = this.themeExamineService!!.listExamineThemeService()
        return JsonUtils.returnMassageUtils(list.isNotEmpty(), list.size)
    }

    /**
     * 审核通过否
     */
    @RequestMapping("passExamine")
    @ResponseBody
    fun passExamineThemeAction(tid: String, state: Int, content: String, request: HttpServletRequest): Map<String, Any> {
        var bool = false
        val msg = when (state) {
            2 -> "通过"
            -1 -> "拒绝"
            else ->
                "操作"
        }
        if (state == 2 || state == -1)
            bool = this.themeExamineService!!.passExamineThemeService(tid, state.toString(), content)
        return JsonUtils.returnMassageUtils(bool, msg + if (bool) "成功" else "失败")
    }


    /**
     * 预览
     */
    @RequestMapping("looktheme")
    fun lookThemeAction(tid: String, request: HttpServletRequest): String {
        var theme = this.themeExamineService!!.lookThemeService(tid)
        request.setAttribute("theme", theme)
        return "admin/index/themepreview"
    }

    /**
     * 预览截图
     */
    @RequestMapping("lookThemeScreenshot")
    fun lookThemeScreenshotAction(tid: String, request: HttpServletRequest): String {
        return "admin/index/themeScreenshot"
    }


    /**
     * 查看全部在线的主题
     */
    @RequestMapping("listTheme")
    fun listThemeAction(request: HttpServletRequest): String {
        val list = this.themeExamineService!!.listThemeService()
        request.setAttribute("dataMap", list)
        return "WEB-INF/admin/examine/listTheme"
    }

    /**
     * 启用与下线主题
     */
    @RequestMapping("useExamine")
    @ResponseBody
    fun useThemeAction(id: String, state: Boolean, request: HttpServletRequest): Map<String, Any> {
        val bool = this.themeExamineService!!.useThemeService(id, (!state).toString())
        val msg = if (state) "下线" else "启用"
        return JsonUtils.returnMassageUtils(bool, msg + if (bool) "成功" else "失败")
    }

    /**
     * 下线删除主题，删除主题是指将主题不在列表中显示，上传者可以删除
     */
    @RequestMapping("deleteTheme")
    @ResponseBody
    fun deleteThemeAction(id: String, request: HttpServletRequest): Map<String, Any> {
        val bool = this.themeExamineService!!.deleteThemeService(id)
        return JsonUtils.returnMassageUtils(bool, if (bool) "删除成功" else "删除失败")
    }


}